﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace AiFrame.Interface.WinUI.Windows.Controls
{
    public partial class CustomizedListView : ListView
    {
        /// <summary>
        /// Gibt an, ob die Zeilen abwechselnd farbig markiert sind.
        /// </summary>
        public bool AlternateColor { get; set; }

        /// <summary>
        /// Gibt an, ob die Spaltenbreite automatisch angepasst
        /// werden soll.
        /// </summary>
        public bool ResizeColumns { get; private set; }

        public CustomizedListView(bool resizeColumns)
        {
            AlternateColor = true;
            ResizeColumns = resizeColumns;

            ResizeColumns = false;
            GridLines = true;
            FullRowSelect = true;

            InitializeComponent();
            Initialize();

        }

        void CustomizedListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        void CustomizedListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            Color backColor1 = Color.White;
            if (e.ItemIndex % 2 == 1)
                backColor1 = Color.FromArgb(250, 250, 250);

            e.Graphics.FillRectangle(new SolidBrush(backColor1), e.Item.Bounds.Location.X, e.Item.Bounds.Location.Y,
                ClientSize.Width, e.Item.Bounds.Height);

            if (e.Item.Selected)
            {
                e.DrawFocusRectangle();

                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(164, 189, 224)), e.Bounds.X, e.Bounds.Y,
                          e.Bounds.Width, e.Bounds.Height);
            }



            e.Graphics.DrawString(e.Item.Text, e.Item.Font, new SolidBrush(Color.Black), e.Item.Bounds.Location.X, e.Item.Bounds.Location.Y);
        }

        void CustomizedListView_MouseMove(object sender, MouseEventArgs e)
        {
            ListViewItem item = this.GetItemAt(e.X, e.Y);
            if (item != null && item.Tag == null)
            {
                Rectangle bounds = new Rectangle(item.Bounds.X, item.Bounds.Y, item.Bounds.Width + 1, item.Bounds.Height);

                this.Invalidate(bounds);
                item.Tag = "tagged";
            }
        }

        void CustomizedListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            RectangleF rectangleF = new RectangleF(e.SubItem.Bounds.X, e.SubItem.Bounds.Y, e.SubItem.Bounds.Width, e.SubItem.Bounds.Height);
            e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(Color.Black), rectangleF);
        }

        protected override void  OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);

//            if (!AlternateColor)
//                return;
//
//            for (int i = 0; i <= Items.Count - 1; i++)
//            {
//                if (Items[i] != null)
//                {
//                    if ((Items[i].Index % 2) != 0)
//                    {
//                        Items[i].BackColor = Color.FromArgb(242, 242, 242);
//                        Items[i].UseItemStyleForSubItems = true;
//                    }
//                    else
//                    {
//                        Items[i].BackColor = Color.White;
//                    }
//                }
//            }
        }

        private void Initialize()
        {
            View = View.Details;
            FullRowSelect = true;

            if (ResizeColumns)
                this.Resize += new EventHandler(CustomizedListView_Resize);
        }

        void CustomizedListView_Resize(object sender, EventArgs e)
        {
            foreach (ColumnHeader column in Columns)
            {
                column.Width = this.Width/Columns.Count;
            }
        }

        /// <summary>
        /// Fügt eine Spalte hinzu und speichert gleichzeitig ein Objekt als Tag
        /// in dieser Spalte ab.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="caption"></param>
        /// <param name="width"></param>
        /// <param name="tag"></param>
        public ColumnHeader AddTaggedColumn(string key, string caption, int width, object tag)
        {
            ColumnHeader columnHeader = Columns.Add(key, caption, width);
            columnHeader.Tag = tag;

            return columnHeader;
        }
    }
}